// 三、接口

// 1. 基本用法

// const getFullName = ({ firstName, lastName }) => {
//   return `${firstName} ${lastName}`
// }
// console.log(getFullName({ firstName: 'jb', lastName: 18 }));

interface NameInfo {
  firstName: string,
  lastName: string
}
const getFullName = ({ firstName, lastName }: NameInfo): string => {
  return `${firstName} ${lastName}`
}
// console.log(getFullName({ firstName: 'jb', lastName: 'lp' }));

interface Vegetable {
  color?: string,// 可选属性
  readonly type: string,// 对象属性只读
  [prop: string]: any,// 索引签名
}
const getVegetables = ({ color, type }: Vegetable) => {
  return `A ${color ? (color + ' ') : ''}${type}`
}
// 多余属性解决办法（3种）

// 类型断言
// console.log(getVegetables({
//   type: 'tomato',
//   color:'red',
//   size:2
// } as Vegetable));

// 索引签名
// console.log(getVegetables({
//   type: 'tomato',
//   color:'red',
//   size:2
// }));

// 类型兼容性
// 先赋值给一个变量
const vegetableInfo = {
  type: 'tomato',
  color: 'red',
  size: 2
}
// console.log(getVegetables(vegetableInfo));

// 数组元素只读
interface ArrInter {
  0: number,
  readonly 1: string
}
let arr5: ArrInter = [1, '2']
// arr5[1] = '3'

// 函数形式
interface addFunc {
  (num1: number, num2: number): number
}

const add: addFunc = (n1, n2) => n1 + n2

// 索引类型

// interface RoleDic {
//   [id: number]: string
// }
// const role1: RoleDic = {
//   'a': 'super_admin'
// }

interface RoleDic {
  [id: string]: string
}
const role2: RoleDic = {
  'a': 'super_admin',
  1: 'admin'
}

// 接口的继承
interface Vegetables {
  color: string
}
interface Tomato extends Vegetables {
  radius: number
}
interface Carrot extends Vegetables {
  color: string,
  length: number
}

const tomato: Tomato = {
  radius: 1,
  color: 'red'
}
const carrot: Carrot = {
  length: 2,
  color: 'orange'
}

// 混合类型接口
interface Counter {
  (): void,
  count: number
}

const getCounter = (): Counter => {
  const c = () => { c.count++ }
  c.count = 0
  return c
}
const counter:Counter = getCounter()

counter()
console.log(counter.count);
counter()
console.log(counter.count);
counter()
console.log(counter.count);


